﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain.Business;
using Actuals.Common;
using Actuals.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;

namespace Actuals
{
    public partial class ShowEmployees : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindGridView();



            }
            else
            {
                if (Request.Form["__EVENTTARGET"] == "refresh")
                {
                    BindGridView();
                }
            }
            this.Page.ClientScript.GetPostBackEventReference(lblMsg, string.Empty);
        }

        protected void BindGridView()
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IList<IEmployee> employees = businessEmployee.GetEmployees();

            gvResults.DataSource = employees;
            gvResults.DataBind();
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string employeeId;
            switch (e.CommandName)
            {
                case "EditEmployee":

                    employeeId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "UpdateAddEmployee", "UpdateAddEmployee(' " + employeeId + " ');", true);
                    break;



            }
        }

        protected void btnDeleteEmployee_Click(object sender, EventArgs e)
        {
            try
            {

                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkShowEmployee");


                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblEmployeeId");

                    if (cb.Checked)
                    {
                        string employeeId = lbl.Text;
                        IBusinessEmployee businessEmployee = new BusinessEmployee();
                        businessEmployee.DeleteEmployee(int.Parse(employeeId));

                        //    IBusinessProject businessProject = new BusinessProject();
                        //  businessProject.DeleteProject(int.Parse(projectId));

                    }
                }

                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
    }
}